package com.dbms.service.impl;

import com.dbms.common.annotation.DataSource;
import com.dbms.common.enums.DataSourceType;
import com.dbms.domain.entity.User;
import com.dbms.mapper.UserMapper;
import com.dbms.service.UserService;
import com.dbms.utils.SnowflakeIdGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    final UserMapper userMapper;
    final SnowflakeIdGenerator snowflakeIdGenerator;

    @Override
    @DataSource(DataSourceType.MYSQL)
    public List<User> getMySQLUsers() {
        return userMapper.selectList(null);
    }

    @Override
    @DataSource(DataSourceType.SQLSERVER)
    public List<User> getSQLServerUsers() {
        return userMapper.selectList(null);
    }

    @Override
    @DataSource(DataSourceType.MYSQL) // 默认使用MySQL
    public void addUser(User user) {
        user.setId(snowflakeIdGenerator.nextId());
        userMapper.insert(user);
    }
}
